/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import com.jme3.animation.AnimChannel;
import com.jme3.animation.AnimControl;
import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;

import com.jme3.math.Vector3f;
import com.jme3.scene.Node;
import java.util.Random;
/**
 *
 * @author prathyush
 */

import com.jme3.scene.Spatial;

public class Enemy extends People implements Runnable {
    private AnimChannel channel;
    private AnimControl control;
    private int damage;
    private Player target;
    private Player locked;
    private int fieldofvision;
    private int threadcontrol = 0;

    Runnable walk = new Runnable(){
        public void run(){
            Random generator = new Random();
            while(true){
                int r = generator.nextInt(4);
                if (r == 0)
                    character.setWalkDirection(new Vector3f(0,0,0.25f));

                else if (r == 1)
                    character.setWalkDirection(new Vector3f(0.25f,0,0));
                else if (r == 2)
                    character.setWalkDirection(new Vector3f(0,0,-0.25f));
                else
                    character.setWalkDirection(new Vector3f(-0.25f,0,0));
                if(character.getPhysicsLocation().y <= -10){
                    character.setPhysicsLocation(new Vector3f(0,15,0));
                }
                character.setViewDirection(character.getWalkDirection());
                try{
                    sleep(5000);
                }catch(InterruptedException e){}
            }
        }
    };
    Runnable shooter = new Runnable(){

        public void run() {
            while(true){
                double myX = character.getPhysicsLocation().x;
                double myZ = character.getPhysicsLocation().z;
                double targetX = target.getControl().getPhysicsLocation().x;
                double targetZ = target.getControl().getPhysicsLocation().z;
                double distance = Math.pow(myX - myZ, 2) + Math.pow(targetX - targetZ, 2);
                Vector3f targetdir = target.getControl().getViewDirection();
                if(threadcontrol == 100){
                    if(distance <= fieldofvision){
                        if(!dead){
                            target.changeHealth(-3);
                            locked = target;
                        }
                    }
                    else{
                        locked = null;

                    }
                    threadcontrol = 0;
                }
                
                if(distance <= fieldofvision){
                    character.setViewDirection(targetdir.mult(-1));
                }
                try{
                    sleep(10);
                }catch (InterruptedException e){}
                
                threadcontrol++;
            }
        }
        
    };
    public Enemy(int fov,Node rootNode, Spatial mod, Vector3f spawnpoint, int num, Player player){
        super(mod, rootNode, spawnpoint, new CapsuleCollisionShape(1.5f, 3f, 1));
        model.setName("enemy"+num);
        model.setLocalScale(1f);
        target = player;
        damage = 100;
        fieldofvision = fov;
        Thread walkcontrol = new Thread(walk);
        Thread shootcontrol = new Thread(shooter);
        shootcontrol.start();
        walkcontrol.start();   
    }
    public void checkDeath(Player target){
        if(character.getPhysicsLocation().y <= -20 || health <= 0){
            character.setPhysicsLocation(new Vector3f(0f,30f,10f));
            if(health <= 0){
                target.getGun().addAmmo(5);
                target.addMoney(10);
            }
            health = 100;
        }
    }
    
    public void respawn(Node root){
        if (dead == true){
            root.attachChild(model);
            health = 100;
            dead = false;
        }
    }
    public boolean isDead(){
        if(dead == true){return true;}
        else{return false;}
    }
    
}
